Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrates autograd integration with NVFuser multidevice #3787

Merged
merged 6 commits into from
Feb 12, 2025

Conversation

syed-ahmed
Copy link
Contributor

@syed-ahmed syed-ahmed commented Jan 29, 2025

This PR demonstrates how to wrap a forward and a backward fusion definition in a torch.autograd.Function that takes PyTorch DTensors as input and outputs PyTorch DTensors.

@wujingyue
Copy link
Collaborator

Cool -- add me to reviewers when it's ready!

@syed-ahmed
Copy link
Contributor Author

@wujingyue To review.

@syed-ahmed
Copy link
Contributor Author

Oops I can't add to the reviewers list.

@wujingyue wujingyue self-requested a review January 29, 2025 22:52
Copy link
Collaborator

@wujingyue wujingyue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM otherwise

tests/python/test_dtensor.py Outdated Show resolved Hide resolved
self.s = sequence
self.e = hidden

class LinearForwardDefinition(FusionDefintionArguments):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel using class and inheritance is an overkill. Functions and partials should be good enough.

def define_linear_forward(config: LinearConfig, fd: FusionDefinition) -> None:

and later

partial(define_linear_forward, config)

):
b, s, e = input._local_tensor.shape
d = weight.device_mesh.size()
op = FusionDefinitionWrapper(LinearForwardDefinition(d, b, s, e))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to construct the op in __init__? Example: https://github.com/canqin001/PointDAN/blob/5001b38cb5506b1c6b40ad1329c1d6f4fbbdd26d/Model.py#L29. I'm worried about the overhead of constructing FusionDefinitionWrapper for each forward and backward call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can create init for torch.autograd.Function: https://github.com/pytorch/pytorch/blob/main/torch/autograd/function.py#L498-L506. Also looks like the code from the link is quite old. I get the following error when trying to add __init__ and use .apply:

# When calling, LinearFunction(d, b, s, e).apply(inp_dtensor, weight_dtensor, bias_dtensor)

[rank0]: Traceback (most recent call last):
[rank0]:   File "/opt/home/dtensor_extension/test.py", line 207, in <module>
[rank0]:     out_dtensor = LinearFunction(d, b, s, e).apply(inp_dtensor, weight_dtensor, bias_dtensor)
[rank0]:                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/usr/local/lib/python3.12/dist-packages/torch/autograd/function.py", line 575, in apply
[rank0]:     return super().apply(*args, **kwargs)  # type: ignore[misc]
[rank0]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[rank0]:   File "/opt/home/dtensor_extension/test.py", line 161, in forward
[rank0]:     outputs = self.forward_op([input, weight, bias])
[rank0]:               ^^^^
[rank0]: NameError: name 'self' is not defined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for confirming! We don't need to figure this out for this PR, but I still wonder how to avoid creating a FusionDefinitionWrapper for each call.

tests/python/test_dtensor.py Outdated Show resolved Hide resolved
tests/python/test_dtensor.py Outdated Show resolved Hide resolved
tests/python/test_dtensor.py Outdated Show resolved Hide resolved
tests/python/test_dtensor.py Outdated Show resolved Hide resolved
@syed-ahmed
Copy link
Contributor Author

!test

@syed-ahmed syed-ahmed requested a review from wujingyue February 11, 2025 01:14
@syed-ahmed
Copy link
Contributor Author

!test

@syed-ahmed
Copy link
Contributor Author

@wujingyue Addressed the review.

@wujingyue
Copy link
Collaborator

There are some real errors in nvfuser-ci/jit_python_distributed_tests_20_A100.

@syed-ahmed
Copy link
Contributor Author

!test

@syed-ahmed
Copy link
Contributor Author

@wujingyue This is ready to be merged.

@wujingyue wujingyue merged commit b0d36aa into NVIDIA:main Feb 12, 2025
52 of 53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants